import 'package:geolocator/geolocator.dart';
import 'package:spring_fairy/util/get_current_position.dart';
import 'package:latlong2/latlong.dart';
import 'amap_api.dart';

class WeatherData {
  final String weather;
  final double temperature;

  WeatherData(this.weather,this.temperature);
}

Future<WeatherData?> getCurrentWeather(Position? position) async {
  if(position == null) {
    position = await getCurrentPosition();
  }

  var weatherMap = await getWeather(LatLng(position.latitude, position.longitude));
  String weather = weatherMap['forecasts'][0]['casts'][0]['dayweather'];
  double? weatherTemp = double.tryParse(weatherMap['forecasts'][0]['casts'][0]['daytemp_float'].toString());

  if(weatherTemp == null) {
    weatherTemp = 21;
  }

  return WeatherData(weather, weatherTemp);
}